package com.weed.client.login;

import java.awt.Dimension;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import java.awt.Rectangle;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.Icon;
import javax.swing.JButton;
import javax.swing.JPanel;
import javax.swing.JViewport;
  
public class ToolBarPanel extends JPanel {

    private static final long serialVersionUID = 1L;
    private static ToolBarPanel toolBarPanel = null;
    private JButton backButton = null;
    private JButton forwordButton = null;
    private JViewport viewPort = null;
    private JPanel toolButtonPanel = null;
     private
     ToolBarPanel(){

        toolBarPanel = this;
        toolBarPanel.setLayout(new GridBagLayout());

        GridBagConstraints backGbConstraints = new GridBagConstraints();
        backGbConstraints.gridx = 0;
        backGbConstraints.gridy = 0;
        backGbConstraints.anchor = GridBagConstraints.WEST;
        backGbConstraints.insets = new Insets(1,1,1,1);

        GridBagConstraints forwardConstraints = new GridBagConstraints();
        forwardConstraints.gridx = 2;
        forwardConstraints.gridy = 0;
        forwardConstraints.anchor = GridBagConstraints.EAST;
        forwardConstraints.insets = new Insets(1,1,1,1);

        GridBagConstraints centerConstraints = new GridBagConstraints();
        centerConstraints.gridx = 1;
        centerConstraints.gridy = 0;
        centerConstraints.fill = GridBagConstraints.BOTH;
        centerConstraints.weightx = 1;
        centerConstraints.weighty = 1;
        centerConstraints.insets = new Insets(1,1,1,1);

        toolBarPanel.add(getBackButton(), backGbConstraints);
        toolBarPanel.add(getForwardButton(), forwardConstraints);
        toolBarPanel.add(getViewPort(), centerConstraints);
        toolButtonPanel.setOpaque(false);
    }

    public static ToolBarPanel getToolBarPanel(){
        if(toolBarPanel == null){
            toolBarPanel = new ToolBarPanel();
        }
        return toolBarPanel;
    }


    private JButton getBackButton(){
        if(backButton == null){
            backButton = new JButton(){
                private static final long serialVersionUID = 1L;
                public Insets getInsets(){
                    return new Insets(0,0,0,0);
                }
            };
            backButton.setText("<");
            backButton.setToolTipText("Back");
            backButton.setIcon(null);
            backButton.setPreferredSize(new Dimension(21,21));
            backButton.setMinimumSize(new Dimension(21,21));
            backButton.addActionListener(new ActionListener(){
                public void actionPerformed(ActionEvent e) {
                    backAction(e);
                }

            });

        }
        return backButton;
    }


    public void setBackButtonStyle(String strButtonName, String strToolTip, Icon icon, Dimension dimesion){
        getBackButton().setText(strButtonName);
        getBackButton().setToolTipText(strToolTip);
        getBackButton().setIcon(icon);
        getBackButton().setPreferredSize(dimesion);
        getBackButton().setMinimumSize(dimesion);
    }


    private JButton getForwardButton(){
        if(forwordButton == null){
            forwordButton = new JButton(){
                private static final long serialVersionUID = 1L;
                public Insets getInsets(){
                    return new Insets(0,0,0,0);
                }
            };
            forwordButton.setText(">");
            forwordButton.setToolTipText("Forward");
            forwordButton.setIcon(null);
            forwordButton.setPreferredSize(new Dimension(21,21));
            forwordButton.setMinimumSize(new Dimension(21,21));
            forwordButton.addActionListener(new ActionListener(){
                public void actionPerformed(ActionEvent e) {
                    forwardAction(e);
                }
            });
        }
        return forwordButton;
    }


    public void setForwardButtonStyle(String strButtonName, String strToolTip, Icon icon, Dimension dimesion){
        getForwardButton().setText(strButtonName);
        getForwardButton().setToolTipText(strToolTip);
        getForwardButton().setIcon(icon);
        getForwardButton().setPreferredSize(dimesion);
        getForwardButton().setMinimumSize(dimesion);
    }


    private JViewport getViewPort(){
        if(viewPort == null){
            viewPort = new JViewport();
            viewPort.add(getToolButtonPanel());
            viewPort.setOpaque(false);
        }

        return viewPort;
    }


    private JPanel getToolButtonPanel(){
        if(toolButtonPanel == null){
            toolButtonPanel = new JPanel();
            toolButtonPanel.setLayout(new GridBagLayout());
            toolButtonPanel.setOpaque(false);
        }
        return toolButtonPanel;
    }


    public void addButtonToToolBar(JButton button){
        if(button == null) return;
        GridBagConstraints constraints = new GridBagConstraints();
        constraints.insets = new Insets(0, 0, 0, 1);
        getToolButtonPanel().add(button, constraints);
    }


    public void addButtonToToolBar(String strButtonName, String strToolTip, Icon icon, ActionListener l, Dimension dimension){
        JButton button = new JButton(){
            private static final long serialVersionUID = 1L;
            public Insets getInsets(){
                return new Insets(0,0,0,0);
            }
        };
        button.setText(strButtonName);
        button.setToolTipText(strToolTip);
        button.setIcon(icon);
        button.addActionListener(l);
        if(dimension == null){
            button.setPreferredSize(new Dimension(60,21));
            button.setMinimumSize(new Dimension(60,21));
        }else{
            button.setPreferredSize(dimension);
            button.setMinimumSize(dimension);
        }

        GridBagConstraints constraints = new GridBagConstraints();
        constraints.insets = new Insets(0, 0, 0, 1);
        getToolButtonPanel().add(button, constraints);
    }


    private void backAction(ActionEvent e){
        Rectangle rec =  getViewPort().getVisibleRect();
        rec.setLocation(rec.getLocation().x + getToolButtonPanel().getComponent(0).getWidth(), rec.getLocation().y);
        getViewPort().scrollRectToVisible(rec);
        getViewPort().updateUI();
    }


    private void forwardAction(ActionEvent e){
        Rectangle rec =  getViewPort().getVisibleRect();
        rec.setLocation(rec.getLocation().x - getToolButtonPanel().getComponent(0).getWidth(), rec.getLocation().y);
        getViewPort().scrollRectToVisible(rec);
        getViewPort().updateUI();
    }


}

